package org.stan.beanvalidator.factory;


import org.stan.beanvalidator.bean.ValidateMsg;
import org.stan.beanvalidator.bean.ValidateResult;
import org.stan.beanvalidator.deletage.CHNPostCodeValidator;
import org.stan.beanvalidator.deletage.IPostCodeDeletage;
import org.stan.beanvalidator.deletage.JPNPostCodeValidator;
import org.stan.beanvalidator.deletage.USAPostCodeValidator;
import org.stan.beanvalidator.factory.validator.*;
import org.stan.beanvalidator.utils.ValidateConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单例工厂：饿汉式
 * 单例模式加载的信息过多，所以首次初始化会比较慢，这也是单例的缺点
 * @author src
 * @Date 2020/6/29 10:27
 * @version 1.0
 */
public class BeanValidateSingletonFactory {
    /** 类加载时直接初始化 **/
    private static final BeanValidateSingletonFactory INSTANCE = new BeanValidateSingletonFactory();
    /** 校验结果 **/
    private static ValidateResult validateResult = new ValidateResult();
    /** 校验失败信息集合 **/
    private static List<ValidateMsg> failedList = new ArrayList<>();
    /** 所有普通工厂生产者容器 **/
    private static Map<String, AnnotationFactoty> map = new HashMap<>();
    /** 所有委派执行者容器 **/
    private static Map<String, IPostCodeDeletage> deletageMap = new HashMap<>();
    /** 默认构造器私有化，防止外部实例化对象 **/
    private BeanValidateSingletonFactory(){}

    /**
     * 静态代码块容器保存所有具体工厂实现类
     * @author src
     * @Date 2020/6/29 9:50
     * @version 1.0
     */
    static {
        /** 初始化所有校验器工厂 **/
        map.put(ValidateConstant.Default_Factory,new DefaultValidator());
        map.put(ValidateConstant.Email,new EmailValidator());
        map.put(ValidateConstant.IDCard,new IDCardValidator());
        map.put(ValidateConstant.Mobile,new MobileValidator());
        map.put(ValidateConstant.NotEmpty,new NotEmptyValidator());
        map.put(ValidateConstant.Number,new NumberValidator());
        map.put(ValidateConstant.Pattern,new PatternValidator());
        map.put(ValidateConstant.Tel,new TelValidator());
        map.put(ValidateConstant.PostCode,new PostCodeValidator());
    }

    static {
        /** 初始化邮编委派执行者 **/
        deletageMap.put(ValidateConstant.Deletage_CHN,new CHNPostCodeValidator());
        deletageMap.put(ValidateConstant.Deletage_USA,new USAPostCodeValidator());
        deletageMap.put(ValidateConstant.Deletage_JPN,new JPNPostCodeValidator());
    }

    /** 获取委派执行者容器 **/
    public Map<String, IPostCodeDeletage> getDeletageMap() {
        return this.deletageMap;
    }

    /** 返回实例 **/
    public static BeanValidateSingletonFactory getInstance(){
        return INSTANCE;
    }

    /** 获取工厂容器 **/
    public Map<String, AnnotationFactoty> getMap() {
        return this.map;
    }
    /** 获取校验失败集合 **/
    public List<ValidateMsg> getFailedList() {
        return this.failedList;
    }
    /** 添加校验失败集合 **/
    public static void addFaileList(ValidateMsg failedList) {
        BeanValidateSingletonFactory.failedList.add(failedList);
    }
    /** 初始化校验失败集合 **/
    public void refreshFaileList(){
        this.failedList = new ArrayList<>();
        ValidateResult validateResult = new ValidateResult();
        validateResult.setResult(true);
        validateResult.setFailedList(new ArrayList<>());
        this.validateResult = validateResult;
    }
    /** 获取校验结果 **/
    public ValidateResult getValidateResult() {
        if(this.failedList.size()<=0)return this.validateResult;
        this.validateResult.setResult(false);
        this.validateResult.setFailedList(this.failedList);
        return this.validateResult;
    }
}
